import actionTypes from "../../actions/actionTypes"
import util from '../../utils'

// const list = util.getLocal("newList",true) || [];
// console.log(list);
// const list = [];
let defaultState = {
	list: [
		{"title":"康磊",
		"text":"路里却团和最工确值局代老组式们江示应队为意明飞果路年那确风老界工何平形见新从正算提个只事美类离花越候志区",
		"key":"123123","readCount":0},
		{"title":"谢明蒋刚郑艳魏涛林涛崔娟唐静贺霞钱明孔静",
		"text":"年须两话便反重入二到已南以技件调点些海往集容林发律对非厂军日过发么九委两处被儿要听美北管效传记照教定业保有际",
		"key":"12334523","readCount":0},
		{"title":"邵磊董磊廖军黎静梁丽萧磊孔娜江娜",
		"text":"酸济运族别示量都东为行处求十战属老形广所治受中青起从化相月片家界必直转天西性加利方会器看且并提地包强员",
		"key":"242123124","readCount":0}
	],
	isLoading: false,
	errMsg: ""
}
export default (state = defaultState, action) => {
	switch (action.type) {
		case actionTypes.START_PATCH_NEWSLIST_STORE:
			return {
				...state,
				isLoading:true
			}
		case actionTypes.PATCH_NEWSLIST_STORE_END:
			return {
				...state,
				isLoading:false
			}
		case actionTypes.PATCH_NEWSLIST_STORE_SUCCESS:
			util.setLocal("newList",action.list);	
			return {
				...state,
				errMsg:"",
				list:action.list
			}
		case actionTypes.PATCH_NEWSLIST_STORE_FAILED:
			return {
				...state,
				errMsg:action.msg
			}
		case actionTypes.SET_NEWSLIST_LOADING_STORE:
			return {
				...state,
				isLoading:action.value
			}
		case actionTypes.ADD_READCOUNT_NEWLIST_STORE:
			let list = Object.assign({},state).list;
			let newList = list.slice();
			newList = newList.map(item=>{
				if(item.key === action.value){
					return item.readCount++
				}else{
					return item
				}
			})
			return {
				...state,
				list:newList
			}
		default:
			return state;
	}
}

